import {
	Tools,
	Modal,
} from 'weaver-mobile';
import React, {
	Component
} from 'react';
import ReactDOM from 'react-dom';
const getLabel = Tools.getLabel;

const isWeakPassword = (params = {}) => Tools.callApi({
	url:'/api/hrm/password/isWeakPassword',
	method:'POST',
	params
});

export const checkWeakPass = (password,onOk,onCancel) => {
	isWeakPassword({
		password
	}).then(datas => {
		const {
			weakPasswordDisable,
			isWeakPassword,
			defaultPasswordEnable
		} = datas;

		if (isWeakPassword) {
			const div = document.createElement("div");
    		document.body.appendChild(div);

			ReactDOM.render(<PassConfirm 
				weakPasswordDisable={weakPasswordDisable}
				onOk={onOk}
				onCancel={onCancel}
			/>,div);
		} else {
			onOk();
		}
	}).catch(() => {
		onOk(); 
	})
}

class PassConfirm extends Component{
	constructor(props){
		super(props);

		this.state = {
			visible:false
		}
	}

	componentDidMount() {
		this.setState({visible:true});
	}

	handleOk = () => {
		const {weakPasswordDisable,onOk}= this.props;

		if (weakPasswordDisable === "0") {
			onOk && onOk();
			this.setState({visible:false});
		}else{
			this.handleCancel();
		}
	}

	handleCancel = () => {
		const {onCancel} = this.props;
		this.setState({visible:false});
		onCancel && onCancel();
	}

	render() {
		const {
			visible
		} = this.state, {
			weakPasswordDisable
		} = this.props;

		if (!visible) {
			return null;
		}

		const btns = [{
			text: getLabel(33703, '确定'),
			onPress: this.handleOk,
		}];

		if (weakPasswordDisable === "0") {
			btns.push({ text: getLabel(32694, '取消'), onPress: this.handleCancel });
		}


		return (
			<Modal 
				closable
				transparent
				visible={visible}
				onClose={this.handleCancel}
				title={getLabel(131329, '信息确认')}
				footer={btns}
			>
              <p >
                  {
                    (weakPasswordDisable === "0") ? getLabel('515419','该密码比较容易被猜到，确定保存吗？')  : getLabel('515420','该密码比较容易被猜到，为了您的系统安全，请重新修改密码。') 
                  }
              </p>
			</Modal>
		)
	}
}

